package com.galen.manual.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.galen.manual.annotation.RequireAdminRole;
import com.galen.manual.common.Result;
import com.galen.manual.dto.BannerCreateDTO;
import com.galen.manual.dto.BannerQueryDTO;
import com.galen.manual.service.BannerService;
import com.galen.manual.vo.BannerListVO;
import com.galen.manual.vo.BannerVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@Tag(name = "轮播图管理", description = "轮播图相关接口")
@RestController
@RequestMapping("/banners")
@RequiredArgsConstructor
public class BannerController {

    private final BannerService bannerService;

    @Operation(summary = "获取Banner列表", description = "分页获取Banner列表（管理员用）")
    @RequireAdminRole(message = "只有管理员可以查看Banner列表")
    @GetMapping
    public Result<Page<BannerListVO>> getBannerPage(
            @Parameter(description = "查询条件") BannerQueryDTO queryDTO) {
        Page<BannerListVO> banners = bannerService.getBannerPage(queryDTO);
        return Result.success(banners);
    }

    @Operation(summary = "获取启用的Banner列表", description = "获取前端展示用的启用Banner列表")
    @GetMapping("/active")
    public Result<List<BannerListVO>> getActiveBanners() {
        List<BannerListVO> banners = bannerService.getActiveBanners();
        return Result.success(banners);
    }

    @Operation(summary = "获取Banner详情", description = "根据ID获取Banner详细信息")
    @RequireAdminRole(message = "只有管理员可以查看Banner详情")
    @GetMapping("/{id}")
    public Result<BannerVO> getBannerDetail(
            @Parameter(description = "Banner ID", required = true) @PathVariable Long id) {
        BannerVO banner = bannerService.getBannerDetail(id);
        return Result.success(banner);
    }

    @Operation(summary = "创建Banner", description = "创建新的Banner")
    @RequireAdminRole(message = "只有管理员可以创建Banner")
    @PostMapping
    public Result<Void> createBanner(
            @Parameter(description = "Banner信息", required = true) @Valid @RequestBody BannerCreateDTO createDTO) {
        bannerService.createBanner(createDTO);
        return Result.success("创建成功");
    }

    @Operation(summary = "更新Banner", description = "更新Banner信息")
    @RequireAdminRole(message = "只有管理员可以更新Banner")
    @PutMapping("/{id}")
    public Result<Void> updateBanner(
            @Parameter(description = "Banner ID", required = true) @PathVariable Long id,
            @Parameter(description = "Banner信息", required = true) @Valid @RequestBody BannerCreateDTO updateDTO) {
        bannerService.updateBanner(id, updateDTO);
        return Result.success("更新成功");
    }

    @Operation(summary = "删除Banner", description = "删除Banner")
    @RequireAdminRole(message = "只有管理员可以删除Banner")
    @DeleteMapping("/{id}")
    public Result<Void> deleteBanner(
            @Parameter(description = "Banner ID", required = true) @PathVariable Long id) {
        bannerService.deleteBanner(id);
        return Result.success("删除成功");
    }

    @Operation(summary = "启用Banner", description = "将Banner状态改为启用")
    @RequireAdminRole(message = "只有管理员可以启用Banner")
    @PutMapping("/{id}/enable")
    public Result<Void> enableBanner(
            @Parameter(description = "Banner ID", required = true) @PathVariable Long id) {
        bannerService.enableBanner(id);
        return Result.success("启用成功");
    }

    @Operation(summary = "禁用Banner", description = "将Banner状态改为禁用")
    @RequireAdminRole(message = "只有管理员可以禁用Banner")
    @PutMapping("/{id}/disable")
    public Result<Void> disableBanner(
            @Parameter(description = "Banner ID", required = true) @PathVariable Long id) {
        bannerService.disableBanner(id);
        return Result.success("禁用成功");
    }

    @Operation(summary = "上传Banner图片", description = "上传Banner图片文件")
    @RequireAdminRole(message = "只有管理员可以上传Banner图片")
    @PostMapping("/upload")
    public Result<String> uploadBannerImage(
            @Parameter(description = "图片文件", required = true) @RequestParam("file") MultipartFile file,
            HttpServletRequest request) {
        try {
            String imageUrl = bannerService.uploadBannerImage(file, request);
            return Result.success("图片上传成功", imageUrl);
        } catch (Exception e) {
            return Result.error("图片上传失败: " + e.getMessage());
        }
    }

    @Operation(summary = "批量更新Banner排序", description = "批量更新Banner的排序值")
    @RequireAdminRole(message = "只有管理员可以更新Banner排序")
    @PutMapping("/sort")
    public Result<Void> updateBannerSort(
            @Parameter(description = "Banner ID列表", required = true) @RequestBody List<Long> bannerIds) {
        bannerService.updateBannerSort(bannerIds);
        return Result.success("排序更新成功");
    }
}